// pages/shop/shop.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [], 
    naviImg: [],
    shop: [],
    list: [],
    isActive: false,
    page: 1,
    page_size: 5,
    no_data: 0,
    onearth: false, // 是否已经到达底部
    latitude: '',
    longitude: '',
    have:true,
    haves:true
  },
  toSeach(){
wx.navigateTo({
  url: '../search/searchs',
})
  },
  // 轮播跳转
  swiper(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  // 点击推荐商家
  selectTap: function() {
    this.setData({
      isActive: false, 
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      have: true,
      haves: true,
      shop: [],
      list: [],
    })
    let latitude = this.data.latitude,
      longitude = this.data.longitude;
    this.getShopList(latitude, longitude)
  },
  // 点击离我最近
  selectTaps: function() {
    this.setData({
      isActive: true,
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      have: true,
      haves: true,
      shop: [],
      list: [],
    })
     let latitude = this.data.latitude,
      longitude = this.data.longitude;
    this.getShopLists(latitude, longitude)
  },
  // 获取地理位置
  getLocation(){
    let that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度  
      success: function (res) {
        console.log(res)
        that.getShopList(res.latitude, res.longitude)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })
  },
  //轮播
  getBannerList() { 
    let that = this;
    wh.Post('/api/common.banner/get_list', { type:2}, function(res) {
      console.log(res)
      that.setData({
        imgUrls: res
      })
    });
  },
  //商品导航列表
  getShopCategory() {
    let that = this;
    wh.Post('/api/mall.shop/category', {
      par_id: 0
    }, function(res) {
      let length = res.length,
        classifydata = [];
      for (let i = 0; i < (length / 8); i++) {
        let data = [];
        for (let j = (i * 8); j < ((i + 1) * 8); j++) {
          data.push(res[j]);
        };
        classifydata.push(data);
      }
      that.setData({
        naviImg: classifydata
      })
    })
  },
  //推荐商家
  getShopList(latitude, longitude) {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    var recomendUrl = '/api/mall.shop/lists';
    if (no_data == 0) {
      wh.Post(recomendUrl, {
        page: page,
        page_num: page_size,
        is_recom: 1,
        lat: latitude,
        lng: longitude,
      }, function(res) {
            console.log(res)
        let shop = that.data.shop.concat(res),
          count = res.length;
        console.log(res)
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length >= 1) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          shop: shop
        })
        if (that.data.shop.length==0){
          that.setData({
            have: false,
            haves: true,
          });
        } else {
          that.setData({
            have: true,
            haves: true,
          });
        }
      })
    }
  },
  //离我最近
  getShopLists(latitude,longitude) {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.shop/lists', {
        page: page,
        page_num: page_size,
        lat:latitude,
        lng:longitude,
      }, function(res) {
    console.log(1)
        let list = that.data.list.concat(res),
          count = res.length;
        console.log(1,res)
        if (page_size > count) {
          that.data.no_data = 1;
          if (list.length >= 1) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          list:list
        })
        if (that.data.list.length == 0) {
          that.setData({
            haves: false,
            have: true,
          });
        } else {
          that.setData({
            haves: true,
            have: true,
          });
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getBannerList()
    this.getShopCategory()
    this.getLocation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.app_auth(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      no_data: 0,
      page: 1,
      page_size: 5,
      onearth: false,
      shop: [],
      list:[],
      isActive: false,
    });
    this.getBannerList()
    this.getShopCategory()
    this.getShopList(this.data.latitude, this.data.longitude)
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size,
      isActive = this.data.isActive;
    if (no_data == 0) {
      this.data.page = page + 1;
      if (isActive){
      this.getShopLists(this.data.latitude, this.data.longitude)
      }else{
        this.getShopList(this.data.latitude, this.data.longitude)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})